#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
key ::= row-key column-family column-qualifier flag timestamp
flag ::= DELETE_ROW | DELETE_COLUMN_FAMILY | DELETE_CELL | INSERT

DELETE_ROW           ::= 0x00
DELETE_COLUMN_FAMILY ::= 0x01
DELETE_CELL          ::= 0x02
INSERT               ::= 0xFF
'''

import md5
import sys
import time
import struct

from base64 import b64encode, b64decode
from bisect import bisect, insort, bisect_left, bisect_right


def DEBUG(fmt, *arg) : print fmt % arg

#######################################################################

def assembleKey(row_key, column_family, column_qualifier, flag, timestamp) :
    if column_qualifier is None : column_qualifier = ''
    if timestamp is None : timestamp = int(time.time())

    family = column_family
    qualifier = column_qualifier
    _timestamp  = hex(timestamp)
    #_timestamp = struct.pack('>I', timestamp)
    _flag = hex(flag)

    key = ' '.join((row_key, family, qualifier, _timestamp, _flag))
    return  key
    # assembleKey()

def desembleKey(key) :
    if key is None : return (None,None,None,None,None)

    items = key.split(' ',4)
    row_key = items[0]
    family = items[1]
    qualifier = items[2]
    timestamp = int(items[3], 16)
    #timestamp, = struct.unpack('>I', items[3])
    flag = int(items[4], 16)

    return (row_key, family, qualifier, flag, timestamp)
    # desembleKey()

#######################################################################

def test1() :
    column_family = 'tag'
    column_qualifier = 'test'

    haystack = []
    timestamp = 10000
    for repeats in range(3) :
        for row_key in ('foo','bar','baz') :
            for flag in (0xFF,0x00,0x01,0x02) :
                timestamp += 100
                key = assembleKey(row_key, column_family, column_qualifier, flag, timestamp)
                insort(haystack, key)

    for needle in haystack :
        _row_key, _family, _qualifier, _flag, _timestamp = desembleKey(needle)
        print 'KEY:%s Family:%s Qualifier:%s TimeStamp:%d Flag:0x%02X' % (_row_key, _family, _qualifier, _timestamp, _flag)

    # test1()

def test2() :
    max_row_key = 1048576
    min_row_key = 1

    column_family = 'tag'
    column_qualifier = 'test'
    flag = 0
    timestamp = 0

    # prepare
    keys = []
    for k in xrange(min_row_key, max_row_key + min_row_key, 1) :
        row_key = md5.new(str(k)).hexdigest()
        keys.append( row_key )
    count_records = max_row_key - min_row_key + 1

    keys_encoded = []
    time_start = time.time()
    for row_key in keys :
        key = assembleKey(row_key, column_family, column_qualifier, flag, timestamp)
        keys_encoded.append(key)
    time_stop = time.time()
    time_last = time_stop - time_start
    if 0 == time_last : time_last = 0.001
    tps = count_records / time_last
    DEBUG('ENCODE records:%d  time-last:%.2f seconds  tps:%.2f', count_records, time_last, tps)

    time_start = time.time()
    for key in keys_encoded :
        (_row_key, _family, _qualifier, _flag, _timestamp) = desembleKey(key)
    time_stop = time.time()
    time_last = time_stop - time_start
    if 0 == time_last : time_last = 0.001
    tps = count_records / time_last
    DEBUG('DECODE records:%d  time-last:%.2f seconds  tps:%.2f', count_records, time_last, tps)

    # test2()

#######################################################################

if  __name__ == '__main__' :
    #test1()
    test2()

